package com.du.method;

import cn.hutool.core.collection.CollectionUtil;
import com.du.entity.ConvertParam;
import com.du.enums.WordConstant;
import lombok.Data;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;

/**
 * @Desc :
 * @Author: shuangdu
 * @date : 2024/4/29
 */
public class PropertiesToYml implements ConvertMethod{

    @Override
    public String parse() {
        ConvertParam param = ConvertContext.holder.getParam();
        Properties properties = new Properties();

        try {
            properties.load(new StringReader(param.getSourceText()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 将Properties对象转换为YAML文本
        // 转为树结构
        YamlTree yamlTree = resolveYamlTree(properties);

        return YamlTree.toYamlString(new StringBuilder(), yamlTree);
    }

    private YamlTree resolveYamlTree(Properties properties) {
        Set<String> keys = properties.stringPropertyNames();
        YamlTree root = new YamlTree();
        for (String key : keys) {
            // 递归解析
            loopResolve(key, properties.getProperty(key), 0, root, null);
        }
        return root;
    }

    private void loopResolve(String key, String value, int level, YamlTree tree, String pKey) {
        System.out.println(key);
        tree.setPKey(pKey);
        int index = key.indexOf(".");
        if (index == -1) {
            tree.key.add(key);
            tree.value.put(key, value);
            tree.setLevel(level);
        } else {
            String parentKey = key.substring(0, index);
            if (!tree.key.contains(parentKey)) {
                tree.key.add(parentKey);
                tree.setLevel(level);
            }
            key = key.substring(index + 1);
            YamlTree child = tree.childMap.get(parentKey);
            if (Objects.isNull(child)) {
                child = new YamlTree();
                tree.childMap.put(parentKey, child);
            }
            loopResolve(key, value, ++ level, child, parentKey);
        }
    }

    @Data
    private static class YamlTree {
        private String pKey;
        private Set<String> key = new LinkedHashSet<>();
        private Map<String, String> value = new LinkedHashMap<>();
        private int level = 0;
        private Map<String, YamlTree> childMap = new LinkedHashMap<>();

        public static String toYamlString(StringBuilder sb, YamlTree tree) {
            if (CollectionUtil.isEmpty(tree.getKey())) {
                return WordConstant.EMPTY;
            }
            String[] keys = tree.getKey().toArray(new String[]{});
            for (String k : keys) {
                String v = tree.getValue().get(k);
                sb.append(padding(k, tree.getLevel()))
                        .append(WordConstant.COLON);
                if (Objects.nonNull(v)) {
                    sb.append((WordConstant.BLANK)).append(v).append("\n");
                } else {
                    // map中找
                    YamlTree child = tree.getChildMap().get(k);
                    if (Objects.isNull(child)) {
                        sb.append(WordConstant.EMPTY);
                    } else {
                        //
                        sb.append("\n");
                        toYamlString(sb, child);
                    }
                }
            }

            return sb.toString();
        }

        private static String padding(String k, int level) {
            for (int i = 0; i < level; i++) {
                k = WordConstant.BLANK.concat(k);
            }
            return k;
        }
    }
}
